var express = require('express');
// 创建了一个路由器
var router = express.Router();
// 引用token加密包
// const { createToken } = require("tools-factory-tang/jwt");
const {adminsLogin, adminsReg} = require("../api/admins")
// 登录
router.post('/adminsLogin',async function(req, res, next) {
    const { adminsname, adminspassword }=req.body;

    const data=await adminsLogin( {adminsname, adminspassword} );
    res.send(data)
    // if (data) {
    //     // const token = createToken({ adminsname });
    //     res.send({
    //       status: true,
    //       // adminsname: data.adminsname,
    //       // token,
    //     })
    //   } else {
    //     res.send({
    //       status: false
    //     })
    //   }  
});


// 注册
router.post('/adminsReg',async function(req, res, next) {
    const { adminsname, adminspassword }=req.body;
    const {status}=await adminsReg({ adminsname, adminspassword });
    res.send(
      status
    )
});

//获取数据库api中get方法
const {get,add,getOne,updata,remove,findOne,changeadmins,adminsListAll} =require("../api/admins")

// 获取所有的电影  排片用的
router.get('/adminsListAll', async function (req, res, next) {
    const data = await adminsListAll();
    res.send(data);
  });

router.get('/',async function(req, res, next) {
    const {curPage,eachPage}=req.query;
    const data=await get({curPage,eachPage});
    res.send(data)
});
router.get('/:_id',async function(req, res, next) {
    const{_id}=req.params;
    const data=await getOne(_id);
     res.send(data)
 });

router.post('/add',async function(req, res, next) {
    // console.log(1);
    const {_id,username,password}=req.body;
    const data=await add({_id,username,password});
    console.log(data);
    res.send(data)

});
router.patch('/:_id',async function(req, res, next) {
   const{_id}= req.params;
   const data=req.body;
   await updata({_id},data);
   const result=await getOne(_id);
    res.send(result)
});
// 删除电影
router.post('/remove',async function(req, res, next) {
    const{_id}= req.body;
    await remove({_id});
    res.send({status:true})
});

// 通过id查找学生  :表示动态
router.get('/findOne/:_id', async function (req, res, next) {
    // params去做路径解析
    const { _id } = req.params;
    const data = await findOne(_id);
    res.send(data)
});


// 修改
router.put("/change", async function (req, res, next) {
    const newData = req.body;
    await changeadmins(newData);
    res.send({ status: true });
})
module.exports=router;
